package com.springboot.util.encryption;

import com.springboot.util.config.SystemProperties;
import com.springboot.util.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * @description:
 * @author: zyj
 * @create: 2020-07-22 09:55
 **/
@Slf4j
public class JdkAes {

    private static final String ENCODING = "UTF-8";
    /**加密算法*/
    private static String KEY_ALGORITHM  = "AES";
    /**
     * 签名算法
     */
    public static final String SIGN_ALGORITHMS = "SHA1PRNG";

    /**
     * @Description: AES加密
     * @Param: [value]
     * @return: java.lang.String
     * @Author: zyj
     * @Date: 2020/7/22
     */
    public static String encrypt(String content) {
        SystemProperties systemProperties = SpringUtil.getBean(SystemProperties.class);
        String aesKey = systemProperties.getAesPasd();//aes 加密/解密key

        try {
            SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
            random.setSeed(aesKey.getBytes(ENCODING));

            KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);// 创建AES的Key生产者
            kgen.init(128, random);// 利用用户密码作为随机数初始化出
            //加密没关系，SecureRandom是生成安全随机数序列，password.getBytes()是种子，只要种子相同，序列就一样，所以解密只要有password就行
            SecretKey secretKey = kgen.generateKey();// 根据用户密码，生成一个密钥
            byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥，如果此密钥不支持编码，则返回
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);// 转换为AES专用密钥
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            byte[] byteContent = content.getBytes(ENCODING);
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器
            byte[] result = cipher.doFinal(byteContent);// 加密
            String encode = Base64.encodeBase64String(result);
            return encode;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("AES加密失败：", e);
        }
        return null;
    }

    /**
     * 解密AES加密过的字符串
     *
     * @param content AES、base64 加密过过的内容
     * @return 明文
     */
    public static String decrypt(String content) {
        SystemProperties systemProperties = SpringUtil.getBean(SystemProperties.class);
        String aesKey = systemProperties.getAesPasd();//aes 加密/解密key

        try {
            byte[] base64Count = Base64.decodeBase64(content);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(aesKey.getBytes(ENCODING));

            KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);// 创建AES的Key生产者
            kgen.init(128, random);
            SecretKey secretKey = kgen.generateKey();// 根据用户密码，生成一个密钥
            byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);// 转换为AES专用密钥
            Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
            byte[] result = cipher.doFinal(base64Count);
            return new String(result); // 明文
        } catch (Exception e) {
            e.printStackTrace();
            log.error("AES解密失败：", e);
        }
        return null;

    }


}
